package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.model.Page25Model;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/10/15
 */
@Component
public class PageData25 {

    public String getData(ExcelModel model){
        List<Page25Model> page25s = model.getPage25s();

        Page25 page25 = new Page25();
//        List<String> thisDate = getThisDate();
        List<TempBeen> thisSquareCount = new ArrayList<>();
        List<TempBeen> thisSquareUser = new ArrayList<>();
        List<TempBeen> thisDetailCount = new ArrayList<>();
        List<TempBeen> thisDetailUser = new ArrayList<>();
        List<TempBeen> lastSquareCount = new ArrayList<>();
        List<TempBeen> lastSquareUser = new ArrayList<>();
        List<TempBeen> lastDetailCount = new ArrayList<>();
        List<TempBeen> lastDetailUser = new ArrayList<>();

        for (int i = 0; i < page25s.size(); i++) {
            Page25Model pageModel = page25s.get(i);
            String squareCount = pageModel.getSquareCount();//pv
            String squareUser = pageModel.getSquareUser();//uv
            String detailCount = pageModel.getDetailCount();//xiangqing pv
            String detailUser = pageModel.getDetailUser();//xiangiqng uv
            String date = pageModel.getDate();

            if (i < 7){
                //本周
                thisSquareCount.add(getTempBeen(squareCount,date));
                thisSquareUser.add(getTempBeen(squareUser,date));
                thisDetailCount.add(getTempBeen(detailCount,date));
                thisDetailUser.add(getTempBeen(detailUser,date));
                continue;
            }
            //下周
            lastSquareCount.add(getTempBeen(squareCount,date));
            lastSquareUser.add(getTempBeen(squareUser,date));
            lastDetailCount.add(getTempBeen(detailCount,date));
            lastDetailUser.add(getTempBeen(detailUser,date));
        }


        page25.setTitle(LogicUtil.getTitle(model,25));
        page25.setDataPeriod("");
        page25.setTitle1("本周数据表现：广场页"+getTitle(thisSquareCount,thisSquareUser));
        page25.setTitle2("本周数据表现：详情页"+getTitle(thisDetailCount,thisDetailUser));
        page25.setTitle3("上周数据表现：广场页"+getTitle(lastSquareCount,lastSquareUser));
        page25.setTitle4("上周数据表现：详情页"+getTitle(lastDetailCount,lastDetailUser));
        page25.setTitle11(getDateList().get(1) + getTitle1().get(0));
        page25.setTitle21(getDateList().get(1) + getTitle1().get(1));
        page25.setTitle31(getDateList().get(0) + getTitle1().get(0));
        page25.setTitle41(getDateList().get(0) + getTitle1().get(1));
        page25.setThisDetailCount(thisDetailCount);
        page25.setThisDetailUser(thisDetailUser);
        page25.setThisSquareCount(thisSquareCount);
        page25.setThisSquareUser(thisSquareUser);
        page25.setLastDetailCount(lastDetailCount);
        page25.setLastDetailUser(lastDetailUser);
        page25.setLastSquareCount(lastSquareCount);
        page25.setLastSquareUser(lastSquareUser);

        return JsonUtil.serialize(page25);
    }


    private String getTitle(List<TempBeen> pvs,List<TempBeen> uvs){
        long sum = pvs.stream().mapToLong(TempBeen::getTempCount).sum();
        long sum1 = uvs.stream().mapToLong(TempBeen::getTempCount).sum();
        return "访问次数：'"+ StringUtil.pointFormat(sum)+"'次；访问人数'"+StringUtil.pointFormat(sum1)+"'人";
    }

    private List<String> getTitle1(){
        List<String> list = new ArrayList<>();
        list.add("一键组队广场页面热度");
        list.add("一键组队详情页面热度");
        return list;
    }

    private TempBeen getTempBeen(String data,String date){
        TempBeen tempBeen = new TempBeen();
        tempBeen.setTempName(date);
        tempBeen.setTempCount(Long.parseLong(data.replace(",","")));
        return tempBeen;
    }

    private List<String> getThisDate(){
        List<String> dateList = DateUtil.getDateList();
        List<String> list = new ArrayList<>();
        for (String s : dateList) {
            list.add(s.replace(".","月")+"日");
        }
        return list;
    }

    private List<String> getDateList(){
        return DateUtil.getDateList(2);
    }

    @Data
    private static class Page25{
        private String title;
        private String dataPeriod;
        private String title1;
        private String title11;
        private String title2;
        private String title21;
        private String title3;
        private String title31;
        private String title4;
        private String title41;
        private List<TempBeen> thisSquareCount;
        private List<TempBeen> thisSquareUser;
        private List<TempBeen> thisDetailCount;
        private List<TempBeen> thisDetailUser;
        private List<TempBeen> lastSquareCount;
        private List<TempBeen> lastSquareUser;
        private List<TempBeen> lastDetailCount;
        private List<TempBeen> lastDetailUser;
    }
}
